//package com.rds.task;
//
//import com.rds.entity.BucketData;
//import com.rds.entity.Group;
//import com.rds.entity.Place;
//import com.rds.entity.PlaceState;
//import com.rds.service.BucketDataService;
//import com.rds.service.GroupService;
//import com.rds.service.PlaceService;
//import com.rds.service.PlaceStateService;
//import lombok.extern.slf4j.Slf4j;
//import org.springframework.beans.factory.annotation.Autowired;
//import org.springframework.scheduling.annotation.Scheduled;
//import org.springframework.stereotype.Component;
//import org.springframework.transaction.annotation.Transactional;
//
//import java.time.LocalDateTime;
//import java.util.Collections;
//import java.util.List;
//
//@Component
//@Slf4j
//public class PlaceStateTask {
//    @Autowired
//    private PlaceService placeService;
//    @Autowired
//    private BucketDataService bucketDataService;
//    @Autowired
//    private GroupService groupService;
//    @Autowired
//    private PlaceStateService placeStateService;
//
//    /**
//     * 垃圾点状态计算
//     */
//    @Scheduled(cron = "0 0/10 * * * ?")
////    @Scheduled(cron = "0/5 * * * * ?")
//    @Transactional
//    public void placeState() {
//        //先查出所有的地点
//        List<Place> list = placeService.list();
//        //遍历地点，查出每个地点的使用情况
//        for (Place place : list) {
//            //查出点下每一个垃圾桶的数据
//            List<BucketData> bucketDataList = bucketDataService
//                    .LatestAnalyseByPlaceIds(Collections.singletonList(place.getId()));
//            //计算剩余时间
//            Double expectedFullDuration = 0.0;
//            Integer currentCapacity = 0;
//            Integer maxCapacity = 0;
//            if (bucketDataList != null && bucketDataList.size() > 0) {
//                for (BucketData bucketData : bucketDataList) {
//                    expectedFullDuration += bucketData.getExpectedFullDuration();
//                    currentCapacity += bucketData.getCurrentCapacity();
//                    maxCapacity += bucketData.getMaxCapacity();
//                }
//            }
//            //获取组名
//            Group byId = groupService.getById(place.getGroupId());
//            String groupName = byId.getName();
//            //构建状态数据
//            PlaceState placeState = PlaceState.builder()
//                    .placeId(place.getId())
//                    .placeName(place.getName())
//                    .groupId(place.getGroupId())
//                    .groupName(groupName)
//                    .centerCoordinateLng(place.getCenterCoordinateLng())
//                    .centerCoordinateLat(place.getCenterCoordinateLat())
//                    .expectedFullDuration(expectedFullDuration)
//                    .currentCapacity(currentCapacity)
//                    .maxCapacity(maxCapacity)
//                    .updateAt(LocalDateTime.now())
//                    .build();
//            //查看数据库有无数据
//            placeStateService.getByPlaceId(place.getId());
//            if (placeStateService.getByPlaceId(place.getId()) != null) {
//                placeStateService.updateByPlaceId(placeState);
//            } else {
//                placeStateService.save(placeState);
//            }
//
//        }
//    }
//}
